package com.aurora.oa.work.view;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import com.aurora.oa.R;
import com.aurora.oa.activity.BaseActivity;
import com.aurora.oa.adapter.CommonRecyclerViewAdapter;
import com.aurora.oa.adapter.CommonRecyclerViewHolder;
import com.aurora.oa.customview.ItemDivider;
import com.aurora.oa.listener.OnRequestPatrolShopListListener;
import com.aurora.oa.model.RequestHttpModel;
import com.aurora.oa.model.RequestHttpModelImpl;
import com.aurora.oa.utils.ToastUtil;
import com.aurora.oa.work.bean.PatrolShopListBean;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2017/5/2 0002.
 */
public class SeePatrolShopMainActivity extends BaseActivity{
    private List<PatrolShopListBean> data = new ArrayList<>();
    private CommonRecyclerViewAdapter<PatrolShopListBean> adapter ;
    private Handler handler = new Handler();
    RequestHttpModel requestHttpModel = new RequestHttpModelImpl();
    LinearLayoutManager layoutManager;
    RecyclerView recyclerView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTitlebar(true,"查看巡店",true);
        setMyContentView(R.layout.activity_patrolshop_list);
        initView();
        getData();
    }

    private void initView() {
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new CommonRecyclerViewAdapter<PatrolShopListBean>(this,data) {
            @Override
            public void convert(CommonRecyclerViewHolder h, PatrolShopListBean entity, int position) {
                h.setText(R.id.tv_shop_name,entity.shopname);
                h.setText(R.id.tv_unauditednum, entity.unauditednum+"");
                h.setText(R.id.tv_total, "/"+entity.total+"条");

            }

            @Override
            public int getLayoutViewId(int viewType) {
                return R.layout.item_patrolshop;
            }
        };
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new ItemDivider().setDividerWith(10).setDividerColor(Color.TRANSPARENT));

        adapter.setOnRecyclerViewItemClickListener(new CommonRecyclerViewAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                Intent intent =new Intent(SeePatrolShopMainActivity.this,PatrolShopDetailActivity.class);
                intent.putExtra("id",data.get(position).shopid);
                startActivity(intent);
            }
        });
    }

    //请求数据
    private void getData(){
        requestHttpModel.getPatrolShopListData(new OnRequestPatrolShopListListener() {
            @Override
            public void onRequestPatrolShopListSuccess(List<PatrolShopListBean> patrolShopListBeanList) {
                refreshUI(patrolShopListBeanList);
            }

            @Override
            public void onRequestPatrolShopListFail(String message) {
                ToastUtil.show(getApplicationContext(),message);
            }
        });
    }

    private void refreshUI(List<PatrolShopListBean> patrolShopListBeanList) {
        data.clear();
        data.addAll(patrolShopListBeanList);
        adapter.notifyDataSetChanged();
    }

}
